/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package lime2;

import lights.interfaces.*;

/**
 * A remote operation dealing with tuples.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class RemTupleOp extends RemoteOp {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5149502805666291769L;
	/**
	 * Encapsulated tuples.
	 */
	ITuple[] tuples;

	/**
	 * Constructor method. Uni.
	 * 
	 * @param type
	 *            of the op
	 * @param subtype
	 *            of the op
	 * @param name
	 *            of the involved tuplespace
	 * @param target
	 *            host
	 * @param to
	 *            original TupleOp
	 */
	RemTupleOp(byte type, byte subtype, String name, LimeServerID target,
			TupleOp to) {
		super(type, name);
		this.subtype = subtype;
		this.target = target;
		this.tuples = to.tuples;
		this.result = to.result;
	}

	/**
	 * Constructor method. Multi.
	 * 
	 * @param type
	 *            of the op
	 * @param subtype
	 *            of the op
	 * @param name
	 *            of the involved tuplespace
	 * @param to
	 *            original TupleOp
	 */
	RemTupleOp(byte type, byte subtype, String name, TupleOp to) {
		super(type, name);
		this.subtype = subtype;
		this.tuples = to.tuples;
		this.result = to.result;
	}

	/**
	 * Constructor method. Multi.
	 * 
	 * @param type
	 *            of the op
	 * @param subtype
	 *            of the op
	 * @param name
	 *            of the involved tuplespace
	 * @param targets
	 * 			  of the targets for multicast     
	 * @param to
	 *            original TupleOp
	 */
	RemTupleOp(byte type, byte subtype, String name, LimeServerID[] targets, TupleOp to) {
		super(type, name);
		this.subtype = subtype;
		this.tuples = to.tuples;
		this.targets = targets;
		this.result = to.result;
	}
	
	/**
	 * Changes this message into a REJECT message.
	 */
	public void reject() {
		this.subtype = LimeConstants.REJECT;
		this.tuples = null;
		this.result = null;
	}
	
	/**
	 * Set the correct destination for tuples
	 * @param id
	 * @author Xing Jiankuan
	 */
	public void setDest(LimeServerID id) {
		LimeWrapper.setDest(new Location(id), tuples);
	}

	/**
	 * toString method.
	 * 
	 * @return textual representation
	 */
	public String toString() {
		return "RemTupleOp (" + Translator.translateType(type) + ", "
				+ Translator.translateSubtype(subtype) + ")";
	}
}
